home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr05 / fres20.zip / FRESSRC.ZIP / FRES.C next >
C/C++ Source or Header  |  1993-03-23  |  16KB  |  523 lines

  1. //--------------------------------------------------------------------------
  2. //
  3. // Fres: Free Resources Display Application
  4. // Displays: Date, Time, Windows Mode, Global free memory,
  5. // limiting resource heap, and percentage of free resource heap.
  6. //
  7. // Demonstrates the use of a non-modal dialog as the main window,
  8. // the use of the undocumented function: GetHeapSpaces (Win 3.0),
  9. // and the use of GetFreeSystemsResources (Win 3.1).
  10. //
  11. // Design & Coding March 1992 by Jamie O'Connell
  12. // Copyright (c) 1992 by Jamie O'Connell
  13. //
  14. // 921025 JWO: Now displays free diskspace
  15. // 930321 JWO: Added Configuration Dialog -- MM Sounds
  16. //
  17. //--------------------------------------------------------------------------
  18.  
  19. #include "windows.h"
  20. #include "mmsystem.h"
  21. #include <time.h>
  22. #include <string.h>
  23. #include <dos.h>
  24. #include <stdlib.h>
  25. #include <direct.h>
  26. #include "fres.h"
  27.  
  28. //---------------------------------------------------------------------------
  29. // Global Variables...
  30. //---------------------------------------------------------------------------
  31.  
  32. HINSTANCE hInstance;              // Global instance handle for application
  33. HWND    hwndKill;
  34. UINT    idKill;
  35. int     Millisec = 500;         // Current timer speed in milliseconds
  36. WORD     SwitchCt =     6;         // Number of timer interrupts before switch
  37. HWND    hwndMain;               // Main hwnd.  Needed in callback
  38. char    szDT[32];                // storage to build strings
  39. char    szMem[32];                // storage to build strings
  40. char    szAppName[] = "Fres";   // Name of App  
  41. char    szVerStr[16];
  42. char    szOnTopKey[16];          // Profile key
  43. char    szTmFMKey[16];
  44. char    szTmFDKey[16];
  45. char    szDiskKey[16];
  46. char    szSndNmKey[16];
  47. char    szSndStKey[16];
  48. char    szSndPath[128];
  49. char    szMenuBuf[16];
  50. char    szTmFreeMem[8]   = "3";
  51. char    szTmFreeDisk[8]  = "3";
  52. char    szDriveNm[8]     = "C";
  53. char    szSndState[8];
  54. WORD    wSndState = IDSND0;
  55. WORD    wTimeFM;
  56. WORD    wTimeFD;
  57. WORD    wDriveNum = 3;        // Drive C
  58.  
  59. short   ChimeCt  = 0;
  60. short   Tick     = 0;
  61.  
  62. BOOL    fOnTop = FALSE;
  63. HMENU   hSysMenu;
  64. RECT    rPos;
  65. DLGPROC lpfnAbtDlg;
  66. DLGPROC lpfnCfgDlg;
  67. DLGPROC lpfnDlgProc;
  68.  
  69. //---------------------------------------------------------------------------
  70. // Function declarations
  71. //---------------------------------------------------------------------------
  72.  
  73. BOOL FAR PASCAL DlgProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
  74. BOOL FAR PASCAL CfgDlg(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
  75. BOOL FAR PASCAL AbtDlg(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
  76. BOOL FormatDT(char *szStr);
  77. BOOL FormatMem(char *szStr);
  78. WORD HeapInfo(VOID);
  79. WORD Hp31Info(VOID);
  80.  
  81. //---------------------------------------------------------------------------
  82. // WinMain
  83. //---------------------------------------------------------------------------
  84.  
  85. int PASCAL WinMain(HINSTANCE hInst, HINSTANCE hInstPrev,
  86.                    LPSTR lpstrCmdLine, int cmdShow) {
  87.     MSG  msgMain;
  88.     char tmp[6];
  89.     char szDrive[_MAX_DRIVE];
  90.     char szDir[_MAX_DIR];
  91.     char szFName[_MAX_FNAME];
  92.     char szExt[_MAX_EXT];
  93.         
  94.     // Set the global instance variable
  95.     hInstance = hInst;
  96.     
  97.     LoadString(hInst, IDVERSTR, szVerStr, sizeof(szVerStr));
  98.     LoadString(hInst, IDTMFM,  szTmFMKey,  sizeof(szTmFMKey));
  99.     wTimeFM = GetProfileInt(szAppName, szTmFMKey, DFTTMFM);
  100.     LoadString(hInst, IDTMFD,  szTmFDKey,  sizeof(szTmFDKey));
  101.     wTimeFD = GetProfileInt(szAppName, szTmFDKey, DFTTMFD);
  102.     LoadString(hInst, IDDISK,  szDiskKey,  sizeof(szDiskKey));    
  103.      GetProfileString(szAppName, szDiskKey, "C", szDriveNm, sizeof(szDriveNm));
  104.     wDriveNum = (WORD)(((char)*szDriveNm)-'A'+1);
  105.  
  106.     lpfnDlgProc = (DLGPROC)MakeProcInstance((FARPROC)DlgProc, hInst);
  107.     if ((hwndMain = CreateDialog(hInst, szAppName,
  108.                                 (HWND)NULL, lpfnDlgProc)) == (HWND)NULL)
  109.         return(0);
  110.     
  111.     // Add About Item to Sytem menu
  112.     hSysMenu = GetSystemMenu(hwndMain, FALSE);
  113.     AppendMenu(hSysMenu, MF_SEPARATOR, 0, NULL); 
  114.     LoadString(hInst, IDM_ABOUT, szMenuBuf, sizeof(szMenuBuf));
  115.     AppendMenu(hSysMenu, MF_STRING, IDM_ABOUT, szMenuBuf);
  116.     LoadString(hInst, IDM_CONFIG, szMenuBuf, sizeof(szMenuBuf));
  117.     AppendMenu(hSysMenu, MF_STRING, IDM_CONFIG,    szMenuBuf);
  118.     LoadString(hInst, IDM_ONTOP, szMenuBuf, sizeof(szMenuBuf));
  119.     AppendMenu(hSysMenu, MF_STRING, IDM_ONTOP,  szMenuBuf);
  120.     LoadString(hInst, IDM_SAVLOC, szMenuBuf, sizeof(szMenuBuf));
  121.     AppendMenu(hSysMenu, MF_STRING, IDM_SAVLOC, szMenuBuf);
  122.      
  123.     LoadString(hInst, IDONTOP, szOnTopKey, sizeof(szOnTopKey));
  124.     GetProfileString(szAppName, szOnTopKey, "off", tmp, sizeof(tmp));
  125.     if (_stricmp(tmp, "on")   == 0 ||
  126.         _stricmp(tmp, "yes")  == 0 ||
  127.         _stricmp(tmp, "true") == 0 ||
  128.         _stricmp(tmp, "1")     == 0) {
  129.         fOnTop = TRUE;
  130.         CheckMenuItem(hSysMenu, IDM_ONTOP, MF_CHECKED);
  131.         }
  132.     
  133.     if (waveOutGetNumDevs() > 0) {
  134.         LoadString(hInst, IDSNDST, szSndStKey, sizeof(szSndStKey));
  135.         wSndState = GetProfileInt(szAppName, szSndStKey, IDSND1);
  136.         LoadString(hInst, IDSNDNM, szSndNmKey, sizeof(szSndNmKey));
  137.         GetProfileString("Sounds", szSndNmKey, "Undefined", 
  138.                          szSndPath, sizeof(szSndPath));
  139.         }
  140.        
  141.     if (wSndState != IDSND0) {
  142.         if (lstrcmp(szSndPath, "Undefined") == 0) { // Need to define it
  143.             GetModuleFileName(hInst, szSndPath, sizeof(szSndPath));
  144.             _splitpath(szSndPath, szDrive, szDir, szFName, szExt);
  145.             LoadString(hInst, IDWAVNM, szFName, sizeof(szFName));
  146.             _makepath(szSndPath, szDrive, szDir, szFName, ".WAV");
  147.             strcat(szSndPath,",Fres Chime");
  148.             WriteProfileString("Sounds", szSndNmKey, szSndPath);
  149.             }
  150.         sndPlaySound(szSndNmKey, SND_ASYNC);
  151.         }
  152.    
  153.     // Main message "pump"
  154.     while (GetMessage((LPMSG) &msgMain, NULL, 0, 0)) {
  155.         if (!IsDialogMessage(hwndMain, (LPMSG)&msgMain)) {
  156.             TranslateMessage((LPMSG) &msgMain);
  157.             DispatchMessage((LPMSG) &msgMain);
  158.             }
  159.         }
  160.  
  161.     FreeProcInstance((FARPROC)lpfnDlgProc);
  162.     return(0);
  163.     }
  164.  
  165.  
  166. //===========================================================================
  167. // AbtDlg
  168. //
  169. // Dialog Callback
  170. //
  171. //===========================================================================
  172.  
  173. BOOL FAR PASCAL _export AbtDlg(HWND hwnd, UINT msg, 
  174.                                WPARAM wParam, LPARAM lParam) {
  175.     switch(msg) {
  176.         case WM_INITDIALOG:
  177.             SetDlgItemText(hwnd, IDVERSTR, szVerStr);
  178.             return(TRUE);
  179.  
  180.         case WM_CLOSE:
  181.             EndDialog(hwnd, 0);
  182.             return(TRUE);
  183.  
  184.         case WM_COMMAND:
  185.             switch(wParam) {
  186.                 case IDOK:
  187.                     EndDialog(hwnd, 0);
  188.                     return(TRUE);
  189.                 }
  190.             break;
  191.         }
  192.     return(FALSE);
  193.     }
  194.  
  195. //===========================================================================
  196. // CfgDlg
  197. //
  198. // Dialog Callback
  199. //
  200. //===========================================================================
  201.  
  202. BOOL FAR PASCAL _export CfgDlg(HWND hDlg, UINT msg, 
  203.                                WPARAM wParam, LPARAM lParam) {
  204.     BOOL fTrans;
  205.     
  206.     switch(msg) {
  207.         case WM_INITDIALOG:
  208.             SetDlgItemInt(hDlg, IDTMFM, wTimeFM, FALSE);
  209.             SetDlgItemInt(hDlg, IDTMFD, wTimeFD, FALSE);
  210.             wsprintf(szDriveNm, "%c", (wDriveNum+'A'-1));
  211.             SetDlgItemText(hDlg, IDDISK, szDriveNm);
  212.             SendDlgItemMessage(hDlg, wSndState, BM_SETCHECK, TRUE, 0);
  213.             return(TRUE);
  214.  
  215.         case WM_CLOSE:
  216.             EndDialog(hDlg, 0);
  217.             return(TRUE);
  218.  
  219.         case WM_COMMAND:
  220.             switch(wParam) {
  221.                 case IDOK: 
  222.                     wTimeFM = GetDlgItemInt(hDlg, IDTMFM, &fTrans, FALSE);
  223.                     wTimeFD = GetDlgItemInt(hDlg, IDTMFD, &fTrans, FALSE);
  224.                     GetDlgItemText(hDlg, IDDISK, szDriveNm, sizeof(szDriveNm));
  225.                     wDriveNum = ((char)*szDriveNm)-'A'+1;
  226.                     if (SendDlgItemMessage(hDlg, IDSND1,
  227.                                                 BM_GETCHECK, 0, 0) != 0L)
  228.                        wSndState = IDSND1; // Every hour
  229.                     else if (SendDlgItemMessage(hDlg, IDSND2,
  230.                                                 BM_GETCHECK, 0, 0) != 0L)
  231.                        wSndState = IDSND2; // Every 1/2 hour
  232.                     else if (SendDlgItemMessage(hDlg, IDSND0,
  233.                                                 BM_GETCHECK, 0, 0) != 0L)
  234.                        wSndState = IDSND0; // Off
  235.                                            
  236.                     wsprintf(szTmFreeMem, "%d", wTimeFM);   
  237.                     WriteProfileString(szAppName, szTmFMKey,  szTmFreeMem);
  238.                     wsprintf(szTmFreeDisk, "%d", wTimeFD);
  239.                     WriteProfileString(szAppName, szTmFDKey, szTmFreeDisk);
  240.                     wsprintf(szDriveNm, "%c", (wDriveNum+'A'-1));
  241.                        WriteProfileString(szAppName, szDiskKey,  szDriveNm);
  242.                     wsprintf(szSndState, "%d", wSndState);                       
  243.                     WriteProfileString(szAppName, szSndStKey, szSndState);                      
  244.                     EndDialog(hDlg, 0);
  245.                     return(TRUE);
  246.                     
  247.                 case IDCANCEL:
  248.                     EndDialog(hDlg, 0);
  249.                     return(TRUE);
  250.                  }
  251.             break;
  252.         }
  253.     return(FALSE);
  254.     }
  255.  
  256. //===========================================================================
  257. // WndProc
  258. //
  259. // Window procedure for the sample applications window.
  260. //
  261. //===========================================================================
  262. BOOL FAR PASCAL _export DlgProc(HWND hwnd, UINT msg, 
  263.                                 WPARAM wParam, LPARAM lParam) {
  264.     int xpos, ypos, nWidth, nHeight;
  265.     WORD wCheck;
  266.     char tmp[6];
  267.  
  268.     switch(msg) {
  269.         case WM_INITDIALOG:
  270.             GetWindowRect(hwnd, (LPRECT)&rPos);
  271.             nWidth = rPos.right - rPos.left;
  272.             nHeight = rPos.bottom - rPos.top;
  273.             xpos = GetProfileInt(szAppName, "X", rPos.left);
  274.             ypos = GetProfileInt(szAppName, "Y", rPos.top);
  275.             MoveWindow(hwnd, xpos, ypos, nWidth, nHeight, TRUE);
  276.  
  277.             lpfnAbtDlg  = (DLGPROC)MakeProcInstance((FARPROC)AbtDlg, 
  278.                                                              hInstance);
  279.             lpfnCfgDlg  = (DLGPROC)MakeProcInstance((FARPROC)CfgDlg, 
  280.                                                              hInstance);
  281.  
  282.             if (SetTimer(hwnd, 1, Millisec, NULL) == NULL) {
  283.                 MessageBox(hwnd, "Couldn't create timer.", "Notice!", MB_OK);
  284.                 DestroyWindow(hwnd);
  285.                 PostQuitMessage(0);
  286.                 return(TRUE);
  287.                 }
  288.             hwndKill = hwnd;
  289.             idKill = 1;
  290.  
  291.         case WM_TIMER:
  292.             if (fOnTop & GetWindow(hwnd, GW_HWNDPREV) != hwnd)
  293.                 SetWindowPos(hwnd, NULL, 0, 0, 0, 0,
  294.                       SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE);
  295.             if (FormatDT(szDT)) {
  296.                 SetWindowText(hwnd, szDT);            // Time has changed
  297.                 PostMessage(hwnd, WM_NCPAINT, 0, 0L); // Redo border
  298.                 }
  299.             if (FormatMem(szMem))
  300.                 SetDlgItemText(hwnd, IDMEM, szMem);                
  301.             if (ChimeCt > 0) {
  302.                 if (Tick == 0) { 
  303.                     sndPlaySound(szSndNmKey, SND_ASYNC);
  304.                     --ChimeCt;
  305.                     }
  306.                 Tick = ++Tick % 4; // Modulus 4
  307.                 }
  308.             else 
  309.                 Tick = 0;          // Reset this
  310.             return(TRUE);
  311.  
  312.         case WM_CLOSE:
  313.             KillTimer(hwndKill, idKill);
  314.             idKill = 0;
  315.  
  316.             FreeProcInstance((FARPROC)lpfnAbtDlg);
  317.             FreeProcInstance((FARPROC)lpfnCfgDlg);
  318.             DestroyWindow(hwnd);
  319.             PostQuitMessage(0);
  320.             return(TRUE);
  321.  
  322.         case WM_SYSCOMMAND:
  323.             switch(wParam) {
  324.                 case IDM_ABOUT:
  325.                     DialogBox(hInstance, "About", hwnd, lpfnAbtDlg);
  326.                     return(TRUE);
  327.  
  328.                 case IDM_CONFIG:
  329.                     DialogBox(hInstance, "Config", hwnd, lpfnCfgDlg);
  330.                     return(TRUE);  
  331.                     
  332.                 case IDM_ONTOP:
  333.                     if (fOnTop) {
  334.                         fOnTop = FALSE;
  335.                         wCheck = MF_UNCHECKED;
  336.                         strcpy(tmp, "off");
  337.                         }
  338.                     else {
  339.                         fOnTop = TRUE;
  340.                         wCheck = MF_CHECKED;
  341.                         strcpy(tmp, "on");
  342.                         }
  343.                     CheckMenuItem(hSysMenu, IDM_ONTOP, wCheck);
  344.                     WriteProfileString(szAppName, szOnTopKey, tmp);
  345.                     return(TRUE);
  346.  
  347.                 case IDM_SAVLOC:
  348.                     GetWindowRect(hwndMain, (LPRECT)&rPos);
  349.                     wsprintf((LPSTR)tmp, "%d", rPos.left);
  350.                     WriteProfileString(szAppName, "X", tmp);
  351.                     wsprintf((LPSTR)tmp, "%d", rPos.top);
  352.                     WriteProfileString(szAppName, "Y", tmp);
  353.                     return(TRUE);
  354.  
  355.                 }
  356.             break;
  357.  
  358.         }
  359.     return(FALSE);
  360.     }
  361.  
  362. //---------------------------------------------------------------------------
  363. // FormatDT
  364. //
  365. // Formats The Date and time - Returns TRUE if it has changed.
  366. //---------------------------------------------------------------------------
  367.  
  368. BOOL FormatDT(char *szStr) {
  369.     static short savehr   = -1;
  370.     static short savemin  = -1;
  371.     struct tm *tym;
  372.     time_t timer = 0;
  373.     short  hour;
  374.     short  mini;
  375.     char   suffix[5];
  376.     
  377.     timer = time(&timer);
  378.     tym = localtime(&timer);
  379.  
  380.     hour = tym->tm_hour;
  381.     mini = tym->tm_min;
  382.  
  383.     if ((mini == savemin) && (hour == savehr)) // hasn't changed enough
  384.       return FALSE;
  385.      
  386.     savemin = mini;
  387.     savehr  = hour;
  388.  
  389.     if (hour > 12) {
  390.         hour = hour - 12;
  391.         strcpy(suffix, "pm");
  392.         }
  393.     else if (hour == 12) {
  394.         if (mini == 0)
  395.             strcpy(suffix, "noon");
  396.         else
  397.             strcpy(suffix, "pm");
  398.         }
  399.     else {
  400.         strcpy(suffix, "am");
  401.         if (hour == 0) {
  402.             hour = 12;
  403.             if (mini == 0)
  404.                 strcpy(suffix, "nite");
  405.             }
  406.         }
  407.           
  408.     if ((mini == 0) && (wSndState != IDSND0))  // Hourly
  409.         ChimeCt = hour;
  410.     if ((mini == 30) && (wSndState == IDSND2)) // 1/2 hour
  411.         ChimeCt = 1;
  412.             
  413.     wsprintf((LPSTR)szStr, "%2.2d-%2.2d-%2.2d %2d:%2.2d %s",
  414.                     tym->tm_mon+1, tym->tm_mday, tym->tm_year,
  415.                     hour, mini, (LPSTR)suffix);
  416.            
  417.     return TRUE;
  418.     }
  419.  
  420. //---------------------------------------------------------------------------
  421. // FormatMem
  422. //
  423. // Formats the free memory string - Returns TRUE if it's changed.
  424. //---------------------------------------------------------------------------
  425.  
  426. BOOL FormatMem(char *szStr) {
  427.     static WORD wResav   = 0;
  428.     static WORD savemem  = 0;
  429.     static WORD Display  = DSMEM;
  430.     static WORD wCounter = 0;            // Counter to display when timer goes off
  431.     static long savavail = 0;
  432.        static struct diskfree_t dfree;
  433.  
  434.     long   avail;
  435.     short  nres;
  436.     short  nmod;
  437.     WORD   wResult;
  438.     WORD   mem;
  439.     BOOL   changed = FALSE;
  440.     
  441.     if (wTimeFM == 0) 
  442.        Display = DSDISK;
  443.     else if (wTimeFD == 0)
  444.        Display = DSMEM;
  445.     else {
  446.         switch(Display) {
  447.             case DSMEM:   // Counter goes off every 1/2 second
  448.                 if (++wCounter >= (wTimeFM << 1)) { // Decide When to switch displays
  449.                     wCounter = 0;
  450.                     Display  = DSDISK; // will flip between 0 and 1  
  451.                     changed  = TRUE; 
  452.                     }
  453.                 break;
  454.                 
  455.             case DSDISK:
  456.                 if (++wCounter >= (wTimeFD << 1)) { 
  457.                     wCounter = 0;
  458.                     Display  = DSMEM; // will flip between 0 and 1
  459.                     changed  = TRUE;
  460.                     }
  461.                 break;
  462.             }
  463.         }
  464.         
  465.     switch(Display) {
  466.         case DSMEM: 
  467.             mem = (WORD)(GetFreeSpace(0) >> 10); // get #of KBytes
  468.             if (mem != savemem) {
  469.                 changed = TRUE;
  470.                 savemem = mem;
  471.                 }
  472.             wResult = HeapInfo(); 
  473.             nres    = HIBYTE(wResult);
  474.             nmod    = LOBYTE(wResult);
  475.             if (wResult != wResav) {
  476.                 changed |= TRUE;
  477.                 wResav = wResult;
  478.                 }
  479.             if (changed)
  480.                 wsprintf(szStr, "%s: %5u K   %s: %2.2d%%",
  481.                         (LPSTR)((GetWinFlags() & WF_ENHANCED) ? "Enh" : "Std"), 
  482.                      mem, (LPSTR)((nmod == GDIHP) ? "GDI" : "User"), nres);
  483.             break;
  484.         case DSDISK:
  485.             _dos_getdiskfree(wDriveNum, &dfree);
  486.             avail = ((long) dfree.avail_clusters
  487.                          * (long) dfree.bytes_per_sector
  488.                     * (long) dfree.sectors_per_cluster) >> 10;
  489.             if (avail != savavail) {
  490.                 changed = TRUE;
  491.                 savavail = avail;
  492.                 }
  493.             if (changed)
  494.                 wsprintf(szStr, "Drive %1s: %7lu K",
  495.                         (LPSTR)szDriveNm, avail);
  496.             break;
  497.         }         
  498.  
  499.     return changed;
  500.     }
  501.  
  502. //---------------------------------------------------------------------------
  503. // HeapInfo
  504. //
  505. // Determines the sizes and percentages of the 2 resource heaps  
  506. //
  507. //---------------------------------------------------------------------------
  508.  
  509. WORD HeapInfo(VOID) {
  510.     BYTE  PctGDI, PctUser;
  511.     WORD  RetVal;
  512.  
  513.     PctGDI    = GetFreeSystemResources(GDIHP);
  514.     PctUser = GetFreeSystemResources(USERHP);
  515.  
  516.     if (PctUser < PctGDI)
  517.         RetVal = (WORD)((PctUser << 8) | USERHP);
  518.     else
  519.         RetVal = (WORD)((PctGDI  << 8) | GDIHP);
  520.  
  521.     return RetVal;
  522.     }
  523.